<template>
  <div class="mainpg">
    <div class="main_spac1" style="padding-bottom: 5px">
      <div class="main_white padding10">
        <div class="mainor_form" style="min-height: 40px;">
          <div class="mainor_te">
            <el-button
              type="primary"
              @click="(addedFormVisible = true), (type = 0)"
              >新增供应商</el-button
            >
          </div>
        </div>
        <div class="main_table" style="min-height: 670px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list.data"
            element-loading-text="加载中"
            fit
            highlight-current-row
          >
            <el-table-column
              label="名称"
              prop="name"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="手机"
              prop="mobile"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="状态"
              prop="name"
              :show-overflow-tooltip="true"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.status"
                  @change="((val)=>{elswitch(val,scope.row.id)})"
                  active-color="#03A98E"
                  active-text="开"
                  inactive-text="关"
                  class="mainor_switch"
                ></el-switch>
              </template>
            </el-table-column>
            <el-table-column
              label="创建时间"
              prop="name"
              :show-overflow-tooltip="true"
            >
              <template slot-scope="scope">{{ parseTime(scope.row.created_at,'{y}-{m}-{d}') }}</template>
            </el-table-column>
            <el-table-column
              label="备注"
              prop="remark"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="管理操作"
              align="center"
              fixed="right"
              width="250"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <router-link to="/purchasing/relation">
                  <el-link type="success">查看关系</el-link>
                </router-link>
                <el-link
                  type="primary"
                  @click="
                    (addedFormVisible = true),
                      (type = 1),
                      (id = scope.row.id),
                      edit_assig(scope.$index)
                  "
                  >编辑</el-link
                >
                <el-link
                  type="danger"
                  @click="(id = scope.row.id), delet(scope.$index)"
                  >删除</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <el-pagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
    <!-- 增加和编辑 -->
    <el-dialog
      :title="type == 1 ? '编辑供应商' : '新增供应商'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @close="close"
      :visible.sync="addedFormVisible"
      class="main_dialog dialog500"
    >
      <el-form
        :inline="false"
        :model="info"
        :rules="rules"
        class="demo-form-inline"
        ref="userListFrom"
        label-width="120px"
      >
        <el-form-item label="名称：" prop="name">
          <el-input
            v-model.trim="info.name"
            placeholder="名称"
            clearable
            class="wid300"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系人：" prop="contact">
          <el-input
            v-model.trim="info.contact"
            placeholder="联系人"
            clearable
            class="wid300"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机：" prop="mobile">
          <el-input
            v-model.trim="info.mobile"
            placeholder="手机"
            clearable
            class="wid300"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码：" :prop="type == 1 ? '' : 'password'">
          <el-input
            v-model.trim="info.password"
            show-password
            :placeholder="type == 1 ? '不填为默认密码' : '填写6位以上的密码'"
            clearable
            class="wid300"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注：" prop="remark">
          <el-input
            v-model.trim="info.remark"
            placeholder="备注"
            clearable
            class="wid300"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态：" prop="status">
          <el-switch
            v-model="info.status"
            active-color="#03A98E"
            active-text="开"
            inactive-text="关"
            class="mainor_switch"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="(addedFormVisible = false), close()"
          >取 消</el-button
        >
        <el-button type="primary" @click="setupmine" v-repeatClick>确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  supplier_index,
  supplier_add,
  supplier_edit,
  supplier_delete,
  supplier_confirmDelete,
  supplier_changeStatus
} from "@/api/purchasing/purchasingSupplier";
import rules from "@/rules/purchasing/purchasingSupplier";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { parseTime } from "@/utils/index";

export default {
  components: {},
  data() {
    return {
      // 时间
      parseTime,
      // 增加和编辑
      info: {
        name: "",
        mobile: "",
        password: "",
        contact: "",
        remark: "",
        status: false,
      },
      type: 0,
      id: 0,
      addedFormVisible: false,
      // 验证
      rules,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: {
        list: {
          data: [],
        },
      },
      listLoading: false,
    };
  },
  methods: {
    // 关闭清空表单
    close() {
      this.info = {
        name: "",
        mobile: "",
        password: "",
        contact: "",
        remark: "",
        status: true,
      };
      this.$refs["userListFrom"].clearValidate();
    },
    // 查看价格开关
    elswitch(val, id) {
      this.supplier_changeStatus(val ? 1 : 0, id);
    },
    supplier_changeStatus(val, id) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, { id: id, status: val })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: id, status: val }
      );
      supplier_changeStatus(data).then((res) => {
        if (res.errcode == 0) {
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.supplier_index();
    },
    // 删除
    delet(index) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, { id: this.id })) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: this.id }
      );
      this.$confirm("是否删除供应商?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          supplier_delete(data).then((res) => {
            if (res.errcode == 0) {
              this.$message({
                message: "删除成功",
                type: "success",
              });
              this.list.list.data.splice(index, 1);
            } else if (res.errcode == 40030) {
              this.$confirm("是否强制删除供应商?", "提示", {
                confirmButtonText: "强制删除",
                cancelButtonText: "取消",
                type: "warning",
              })
                .then(() => {
                  supplier_confirmDelete(data).then((res) => {
                    if (res.errcode == 0) {
                      this.list.list.data.splice(index, 1);
                    }
                  });
                })
                .catch(() => {});
            } else {
              this.$message.error(res.errmsg);
            }
          });
        })
        .catch(() => {});
    },
    // 确认
    setupmine() {
      this.$refs["userListFrom"].validate((valid) => {
        if (valid) {
          if (this.type == 1) {
            if (this.info.password != "" && this.info.password.length < 6) {
              this.$message.error("密码为6位以上");
              return;
            }
          }
          this.type == 0 ? this.add() : this.edit();
        }
      });
    },
    // 编辑赋值
    edit_assig(index) {
      this.info = {
        name: this.list.list.data[index].name,
        password: "",
        contact: this.list.list.data[index].contact,
        remark: this.list.list.data[index].remark,
        mobile: this.list.list.data[index].mobile,
        status: this.list.list.data[index].status==1?true:false,
      };
    },
    // 新增保存
    add() {
      let info=JSON.parse(JSON.stringify(this.info))
      info.status=info.status?1:0
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, info)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        info
      );
      supplier_add(data).then((res) => {
        this.addedFormVisible = false;
        if (res.errcode == 0) {
          this.$message({
            message: "新增成功",
            type: "success",
          });
          this.current.page = 1;
          this.supplier_index();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 编辑保存
    edit() {
      let info=JSON.parse(JSON.stringify(this.info))
      info.status=info.status?1:0
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, info, { id: this.id })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        info,
        { id: this.id }
      );

      supplier_edit(data).then((res) => {
        this.addedFormVisible = false;
        if (res.errcode == 0) {
          this.$message({
            message: "编辑成功",
            type: "success",
          });
          this.supplier_index();
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.supplier_index();
    },
    changePage(page) {
      this.current.page = page;
      this.supplier_index();
    },
    // 列表
    supplier_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.current)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.current
      );
      supplier_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          res.data.list.data.map((e) => {
            e.status = e.status==1?true:false;
          });
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
  },
  created() {
    // 列表
    this.supplier_index();
  },
};
</script>
<style lang="scss" scoped>
.mainor_form form {
  width: calc(100% - 120px);
}
</style>
